package com.topboya.linker.ui.adapter;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.OrgLevel0Item;
import com.topboya.linker.model.levelitem.OrgLevel1Item;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.activity.AddCustomerActivity;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.CustomerPresenter;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomerExpandableItemAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    private static final String TAG = "CustomerExpandableItemA:";
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    private BaseActivity mContext;
    private ArrayList<OrganizationResopnse.DocDetailInfoList> grouplist;
    public CustomerExpandableItemAdapter(List<MultiItemEntity> data, BaseActivity context, ArrayList<OrganizationResopnse.DocDetailInfoList> grouplist) {

        super(data);
        this.mContext = context;
        this.grouplist = grouplist;
        addItemType(TYPE_LEVEL_0, R.layout.expandablelist_item0_customer);
        addItemType(TYPE_LEVEL_1, R.layout.expandablelist_item1_customer);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {

        switch (helper.getItemViewType()) {

            case TYPE_LEVEL_0:
                final OrgLevel0Item orglv0 = (OrgLevel0Item) item;
                helper.setText(R.id.tv_hasptial_level, orglv0.getGetHaspatialevel())
                        .setText(R.id.tv_hasptial_name, orglv0.getHaspatialname());

                helper.itemView.setOnClickListener(v -> {
                    int pos = helper.getAdapterPosition();
                    LogUtils.v(TAG +"Level 0 item pos: " + pos);
                    if (orglv0.isExpanded()) {
                        helper.setGone(R.id.img_add_doctor,false);
                        collapse(pos);
                    } else {
                        if (null !=  orglv0.getSubItems()){
                            orglv0.getSubItems().clear();
                        }
//                        new CustomerPresenter(mContext).getCustomerByOrgId(orglv0.getOrgID(), departmentgroupmap -> {
//
//                            for(Map.Entry<String,  List<CustomerByOrgIdResponse.DepartmentList>> entry : departmentgroupmap.entrySet()) {
//                                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
//                                OrgLevel1Item orgLevel1Item = new OrgLevel1Item();
//                                orgLevel1Item.setSubTitle(entry.getKey());
//                                List<String> namelist =new ArrayList<>();
//                                for ( CustomerByOrgIdResponse.DepartmentList departmentgroup:entry.getValue()){
//                                    namelist.add(departmentgroup.getCusName());
//                                }
//                                orgLevel1Item.setNameList(namelist);
//                                orglv0.addSubItem(pos,orgLevel1Item);
//                            }
//                            helper.setGone(R.id.img_add_doctor,true);
//                            expand(pos);
//                        },false);
//
                    }
                });
                helper.getView(R.id.img_add_doctor).setOnClickListener(v -> {
                    int pos = helper.getAdapterPosition();
                    Intent intent = new Intent(mContext,AddCustomerActivity.class);
                    intent.putExtra("DocDetailInfoList", grouplist.get(pos));
                    mContext.startActivity(intent);

                });

                break;

            case TYPE_LEVEL_1:
                LogUtils.v(TAG + "TYPE_LEVEL_1");
                final OrgLevel1Item lv1 = (OrgLevel1Item) item;
                helper.setText(R.id.tv_subtitle, lv1.getSubTitle());
                RecyclerView recyclerView = helper.getView(R.id.expand_item1_doc);
                recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 3));
                recyclerView.setAdapter(new BaseQuickAdapter<String, BaseViewHolder>(R.layout.gridview_item, lv1.nameList) {

                    @Override
                    protected void convert(BaseViewHolder helper, String item) {
                        helper.setText(R.id.tv_gridview, item);
                    }
                });
                break;
        }

    }


    private void getOrgError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "getOrgError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }


}
